Tired of long boring walls of scrolling text? Need to call attention to something important?
Adding a splash of color to your console output makes important events stand out!
There are a couple of ways to add colors to the console output, but neither one is very user friendly.
The most common way is setting Console.ForegroundColor
and Console.BackgroundColor
.
Just
don't forget to Console.ResetColor()
afterwards or all the rest of the output is
colored.
Here's an example:
Console.ForegroundColor = ConsoleColor.Red;
Console.BackgroundColor = ConsoleColor.DarkRed;
Console.WriteLine("Error: Something failed to happen");
Console.ResetColor();
Want to just color the word "Error:"? Totally possible!
Break the output up, set the color, Write (don't WriteLine yet) the word "Error:",
reset
the colors, then WriteLine the rest of the error.
Pretty quickly the Console output becomes complicated and getting that right takes focus away from the actual
code!
You can also use ANSI Escape codes!
Console.WriteLine($"\u001B[91;41mError: Something failed to happen\u001B[0m");
Changing this to the single word version is much easier:
Console.WriteLine($"\u001B[91;41m Error: \u001B[0m Something failed to happen");
Is it maintainable? Not really. What color is '91'? Why is there a '41'? Do you remember the syntax? You can look it up, but how much time do you want to spend looking up syntax for logging???
I'm still using the ANSI escape color codes, that's the way to go. But they're self describing now! It's much
easier
to read and far quicker to write!
(Unless you spend an unusual amount of time using colored output in Minecraft. It uses the same escape codes.)
The above lines would look like this:
Console.WriteLine(Color.Red($"Error: Something failed to happen").WithDarkRed());
Console.WriteLine($"{Color.Red("Error:").WithDarkRed()} Something failed to happen");
and display:
Now if you decide later you don't want the background color, could you figure out how to remove that? Sure you
could!
If you wanted to add a 'Success' message, could you figure out the syntax? I bet you could!
Here are some examples:
Note: The background effect doesn't overwrite the global setting of Console.BackgroundColor
This library adds some methods to help make adding color to the console output easier. These methods return the string wrapped in the ANSI escape codes for you. Since the escape codes work directly on the string, the color changes are 'local' to each line. If you want to set the whole background or default text color, you can still do that.
The foreground colors are methods that take in a string and return the same string wrapped in the ANSI escape
codes.
Simply pass your text into a color method:
Console.WriteLine(Color.Blue("This prints the text in Blue"));
The background is whatever Console.Background is set to.
You don't have to use the entire string. To color a single word, interpolate the command in the string:
Console.WriteLine($"This prints the word {Color.Yellow("'Yellow'")} in Yellow");
The rest of the line is whatever the console is set to.
Console.WriteLine($"There are {Color.Blue("blue skys", ConsoleColor.White)} ahead");
This is limited to the standard ConsoleColors
There's a Color.RGB() method that takes in the string to color, followed by red, green, and blue values. If you
know
the hexadecimal, prefix it with "0x"
Console.WriteLine(Color.RGB("Let's try something new!", 255, 45, 45));
Console.WriteLine(Color.RGB("Let's try something new!", 0x2d, 0xff, 0x2d));
Console.WriteLine(Color.RGB("Let's try something new!", 45, 45, 255));
This is still experimental and doesn't play well with nesting.
The backgound colors are string extension methods and are prefixed by ".with###()" so just add the
background color you want to the end of the string:
Console.WriteLine("The other way around string method".WithBlack());
You can also use this to highlight specific words:
Console.WriteLine($"This is {"highlighted".WithDarkGreen()} in dark green");
When nested, the most immediate commands win out, like most nesting.
You can freely mix and match as much as you want:
Console.WriteLine(Color.DarkRed($" This is pretty {Color.Red("complicated", ConsoleColor.DarkRed)} nesting, but {Color.DarkGreen("perfectly")} fine").WithWhite());
Support for ANSI escape codes around styling is a little spotty. They may or may not work well for your
application.
The 'bold' feature really only works well on dark colors, and all it does is translate them to the lighter
version
of the same color.
Bold:
Console.WriteLine(Color.DarkCyan($" The {Color.Bold(" bold feature ")} really only works on dark colors, it just changes them to the {"light".WithBold()} version"));
Console.WriteLine(Color.Magenta($" The {Color.Bold("bold feature")} doesn't show any difference for already light colors"));
Underline:
Console.WriteLine($" The {Color.Underline("underline")} can be used for emphasis. There are {"both formats".WithUnderline()} of the styling methods");
Inverse: (swaps the background and foreground colors, kinda neat!)
Console.WriteLine($" You can {Color.Invert(" invert a section of text ")} in a line");
Success, writes green on dark green background:
Console.WriteLine($" The operation was a {Color.Success("success")}, please proceed");
Warning, writes yellow on dark yellow background:
Console.WriteLine($" Trouble {Color.Warning("parsing file")}, you may want to check results");
Failure, writes red on dark red background:
Console.WriteLine($" Parsing the arguments {Color.Failure("failed")}, check the logs");
Adding a static using lets you simplify coding even more:
using static ConsoleColors.Color;
.........
Console.WriteLine(DarkRed($"This is a simplified version with {Underline("underline")}. It makes typing a little easier."));